﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Toyger.Common.Connections;
using Toyger.Common.PlugIn;
using Toyger.Common.Interfaces;

namespace Toyger.Common.UI
{
    public partial class ConnObjPane : UserControl
    {
        #region static
        static Dictionary<ConnectionObject, ConnObjPane> cache = new Dictionary<ConnectionObject, ConnObjPane>();
        internal static ConnObjPane GetInstance(ConnectionObject cObj, IDBManager server, IController  controller)
        {
            lock (cache)
            {
                ConnObjPane cp = null;
                if (!cache.TryGetValue(cObj, out cp))
                {
                    cp = new ConnObjPane(cObj, server, controller);
                    cache.Add(cObj, cp);
                }
                return cp;
            }
        }
        #endregion

        ConnectionObject cObj;
        IConnectionEditor connEditor;
        IController controller;
        private ConnObjPane(ConnectionObject cObj, IDBManager server, IController controller)
        {
            this.cObj = cObj;
            this.controller = controller;
            InitializeComponent();
            connEditor = server.GetConnEditor(cObj);
            Control connControl = connEditor.GetControl();
            connControl.Dock = DockStyle.Fill;
            gb_Conn.Controls.Add(connControl);
            
            this.tbName.Text = cObj.Name;
            
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            cObj.Name = tbName.Text;
            connEditor.Save();
            controller.SaveConnectionObject(cObj);
        }

        
    }
}
